#include "loginform.h"
#include "models/command.h"
#include "public/public.h"
#include "qdebug.h"
#include "ui_loginform.h"
#include <QSettings>
#include "version.h"
#include "models/curusermodel.h"

LoginForm::LoginForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LoginForm)
    , m_nLoginTimerCount(-1)
{
    setAttribute(Qt::WA_StyledBackground, true);
    ui->setupUi(this);
    ui->label_version->setText(QString("版本号:")+APP_VERSION);
    QSettings * pIniFile = new QSettings(getUserPath() + "/user.ini", QSettings::IniFormat);
    if (nullptr != pIniFile)
    {
        ui->user_lineEdit->setText(pIniFile->value("user/u").toString());
        ui->pwd_lineEdit->setText(QByteArray::fromBase64(pIniFile->value("user/p").toByteArray()));
        ui->remberPwd_checkBox->setChecked(!ui->user_lineEdit->text().isEmpty());
        ui->autoLogin_checkBox->setChecked(pIniFile->value("user/a").toBool());
        if (ui->autoLogin_checkBox->isChecked())
        {
            ui->login_ret_lab->setStyleSheet("color:#A9A9A9");
            ui->login_ret_lab->setText("正在登陆...");
            ui->login_pushButton->setText(tr("取消登录"));
            m_nLoginTimerCount = 0;
            connect(&m_autoLoginTimer, &QTimer::timeout, this, &LoginForm::onAutoLoginTimer);
            m_autoLoginTimer.start(500);
        }
    }
    delete pIniFile;

    scaleWidgets(this,g_xScale,g_yScale);
}

LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::scaleWidgets(QWidget *widget, double xScale,double yScale)
{
    setFixedSize(int(widget->width() * xScale),int(widget->height() * yScale));
    for (QWidget * child : widget->findChildren<QWidget*>()) {
        QRect geom = child->geometry();
        int width = qMax(1, static_cast<int>(int(geom.width() * xScale)));
        //当按钮高度比较大时使用y方向比例，当按钮比较小时使用x比例，防止按钮被压扁
        int height = qMax(1, static_cast<int>((geom.height() == geom.width() ? int(geom.height() * xScale) : int(geom.height() * yScale))));
        child->setFixedSize(width,height);
        child->move(int(geom.left() * xScale),int(geom.top() * yScale));
        QFont font = child->font();
        if(font.pixelSize()> 0)
            font.setPixelSize(int(font.pixelSize()*xScale));
        else if(font.pointSize()> 0)
            font.setPixelSize(int(font.pointSize()*xScale));
        child->setFont(font);
    }
    qApp->setStyleSheet(QString("QCheckBox::indicator { width: %1px; height: %1px; margin-top:%2px}").arg(int(24*xScale)).arg(int(6*xScale)));
}

void LoginForm::on_Exit_pushButton_clicked()
{
    qApp->exit(0);
}

void LoginForm::on_autoLogin_checkBox_stateChanged(int arg1)
{
    if(arg1 == Qt::Checked)
        ui->remberPwd_checkBox->setCheckState(Qt::Checked);
}

void LoginForm::on_remberPwd_checkBox_stateChanged(int arg1)
{
    if(arg1 == Qt::Unchecked)
        ui->autoLogin_checkBox->setCheckState(Qt::Unchecked);
}

void LoginForm::on_user_lineEdit_focusChanged()
{
    ui->user_label->setStyleSheet(ui->user_lineEdit->hasFocus()?"color:#49A6FF":"color:#A9A9A9");
    ui->user_line->setStyleSheet(ui->user_lineEdit->hasFocus()?"background-color:#49A6FF":"background-color:#A9A9A9");
}


void LoginForm::on_pwd_lineEdit_focusChanged()
{
    ui->pwd_label->setStyleSheet(ui->pwd_lineEdit->hasFocus()?"color:#49A6FF":"color:#A9A9A9");
    ui->pwd_line->setStyleSheet(ui->pwd_lineEdit->hasFocus()?"background-color:#49A6FF":"background-color:#A9A9A9");
}

void LoginForm::onAutoLoginTimer()
{
    ui->login_ret_lab->setText(ui->login_ret_lab->text()+".");
    m_nLoginTimerCount++;
    if(m_nLoginTimerCount == 6)
    {
        m_autoLoginTimer.stop();
        ui->login_pushButton->setText(tr("登 录"));
        ui->login_ret_lab->setText("");
        login();
        m_nLoginTimerCount = -1;
    }
}

bool LoginForm::login()
{
    //检测合法
    ui->login_ret_lab->setText(QObject::tr(""));
    QString strUser = ui->user_lineEdit->text().trimmed();
    if(strUser.length() == 0)
    {
        ui->login_ret_lab->setStyleSheet("color:#FF0000");
        ui->login_ret_lab->setText(QObject::tr("用户名不能为空！"));
        return false;
    }
    QString strPwd = ui->pwd_lineEdit->text();
    //执行登录
    QString strErr = "";

    if(CurUserModel::getInstance()->login(strUser, strPwd,strErr))
    {
        //登录成功
        QSettings * pIniFile = new QSettings(getUserPath() + "/user.ini",QSettings::IniFormat);
        pIniFile->beginGroup("user");
        if(ui->remberPwd_checkBox->checkState())
        {
            pIniFile->setValue("u",strUser);
            pIniFile->setValue("p",QString(strPwd.toLocal8Bit().toBase64()));
            pIniFile->setValue("a",ui->autoLogin_checkBox->checkState());
        }
        else
        {
            pIniFile->remove("u");
            pIniFile->remove("p");
            pIniFile->remove("a");
        }
        pIniFile->endGroup();
        delete pIniFile;
        return true;
    }
    else
    {
        ui->login_ret_lab->setStyleSheet("color:#FF0000");
        ui->login_ret_lab->setText(strErr);
        return false;
    }
}

void LoginForm::on_login_pushButton_clicked()
{
    if(m_nLoginTimerCount >= 0)
    {
        m_autoLoginTimer.stop();
        ui->login_pushButton->setText(tr("登 录"));
        m_nLoginTimerCount = -1;
        ui->login_ret_lab->setText("");
    }
    else
    {
        login();
    }
}

void LoginForm::on_pushButton_clicked()
{
    ui->pwd_lineEdit->setEchoMode(ui->pushButton->isChecked()? QLineEdit::Normal: QLineEdit::Password );
}

